import { projectSetting } from '@pkg/setting';
import { initRequest } from '@pkg/request';
import { initDirective } from '@pkg/directives';
import { initUtils } from '@pkg/utils';
// service
import { getGlobalConfig } from '@/internal';
import { useMessage } from '@/hooks/web/useMessage';
import { useErrorLogStoreWithOut } from '@/store/error-log';
import { useUserStoreWithOut } from '@/store/user';
import { useI18n } from '@pkg/locale';
import { SessionTimeoutProcessingEnum } from '@pkg/tokens';
// directive
import { createLoading } from '@/components/loading';
import { usePermission } from '@/hooks/web/usePermission';

// 为了解耦 `packages/*` 下面各模块，不再相互依赖
// 把常用方法导入到具体使用的子模块中,各个模块需要提供 `bridge` 文件作为解耦方式
export const initPackages = async () => {
  await Promise.all([_initRequest(), _initDirective(),_initUtils()]);
};

const _initUtils = async () => {
  await initUtils({
    getGlobalConfig
  });
};

const _initDirective = async () => {
  const { hasPermission } = usePermission();

  await initDirective({
    hasPermission,
    createLoading,
  });
};

const _initRequest = async () => {
  const { createMessage, createErrorModal, createSuccessModal } = useMessage();
  const { apiUrl, uploadUrl } = getGlobalConfig();
  const { t } = useI18n();
  await initRequest({
    apiUrl,
    uploadUrl,
    getTokenFunction: () => {
      const userStore = useUserStoreWithOut();
      return userStore.getToken;
    },
    getUserStore: () => {
      const userStore = useUserStoreWithOut();
      return userStore;
    },
    errorFunction: createMessage.error,
    successFunction: createMessage.success,
    errorModalFunction: createErrorModal,
    successModalFunction: createSuccessModal,
    errorLogFunction: (error) => {
      const errorLogStore = useErrorLogStoreWithOut();
      errorLogStore.addAjaxErrorInfo(error);
    },
    timeoutFunction: () => {
      const userStore = useUserStoreWithOut();
      userStore.setToken(undefined);
      userStore.logout(true);
    },
    unauthorizedFunction: (msg?: string) => {
      const stp = projectSetting.sessionTimeoutProcessing;
      const userStore = useUserStoreWithOut();
      userStore.setToken(undefined);
      if (stp === SessionTimeoutProcessingEnum.PAGE_COVERAGE) {
        userStore.setSessionTimeout(true);
      } else {
        userStore.logout(true);
      }
      return msg || t('sys.api.errMsg401');
    },
    handleErrorFunction: (message, mode) => {
      if (mode === 'modal') {
        createErrorModal({ title: t('sys.api.errorTip'), content: message });
      } else if (mode === 'message') {
        createMessage.error(message);
      }
    },
  });
};
